#include "ADC.h"

void ADC_Port_Init(void)
{
	rcu_periph_clock_enable(RCU_GPIOC);
	rcu_periph_clock_enable(RCU_ADC0);
	
	gpio_mode_set(GPIOC, GPIO_MODE_ANALOG, GPIO_PUPD_NONE, GPIO_PIN_0);
	
	adc_clock_config(ADC_ADCCK_PCLK2_DIV8);
	ADC_Init();
	adc_software_trigger_enable(ADC0, ADC_ROUTINE_CHANNEL);
}

void ADC_Init(void)
{
	adc_deinit();
	
	adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, ENABLE);
	
	adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
	
	adc_channel_length_config(ADC0, ADC_ROUTINE_CHANNEL, 1);//一个通道
	
    adc_routine_channel_config(ADC0, 0, ADC_CHANNEL_10, ADC_SAMPLETIME_480);

    adc_external_trigger_source_config(ADC0, ADC_ROUTINE_CHANNEL, ADC_EXTTRIG_INSERTED_T0_CH3);   // ADC ԥעƤ׃ì{ԃ֨ʱǷԥע
    adc_external_trigger_config(ADC0, ADC_ROUTINE_CHANNEL, EXTERNAL_TRIGGER_RISING_FALLING);   							  // Ǵԃԥע
	
    adc_enable(ADC0);
	
    delay_1ms(1);

    adc_calibration_enable(ADC0);
}

int ADC_Value;
void ADC_Pro(void)
{
	adc_flag_clear(ADC0, ADC_FLAG_EOC);
	
	while(SET != adc_flag_get(ADC0, ADC_FLAG_EOC)){}
	
	ADC_Value = ADC_RDATA(ADC0);
	//printf("ADC_Value = %d, Vol_Value = %.2f\r\n", ADC_Value, 3.3 * ((double)ADC_Value / 4096));
}
